package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.ec.core.FeedBuilderService;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.model.business.vo.ectemplate.EcElementFilterParam;
import com.ndp.fb.rdb.api.EcElementAttachRdbService;
import com.ndp.fb.rdb.api.EcElementCategoryRdbService;
import com.ndp.fb.rdb.api.EcTemplateElementRdbService;
import com.ndp.fb.rdb.api.EcTemplateRdbService;
import com.ndp.fb.rdb.model.*;
import com.ndp.fb.rdb.model.ecelement.EcElementAttach;
import com.ndp.fb.rdb.model.ectemplate.EcTemplateElementShowResult;
import com.ndp.fb.util.ArrayUtil;
import com.ndp.fb.util.ListUtil;
import com.ndp.fb.walle.business.EcTemplateDetailService;
import com.ndp.fb.walle.model.vo.ectemplate.param.EcTemplatePreviewElementParam;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by evan on 2016/7/4.
 */
@Component
public class EcTemplateDetailServiceImpl implements EcTemplateDetailService {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    EcTemplateElementRdbService ecTemplateElementRdbService;

    @Reference
    EcElementCategoryRdbService ecElementCategoryRdbService;

    @Reference
    FeedBuilderService feedBuilderService;

    @Reference
    EcTemplateRdbService ecTemplateRdbService;

    @Reference
    EcElementAttachRdbService ecElementAttachRdbService;

    @Override
    public List<EcTemplateElementShowResult> filter(EcElementFilterParam param, Long orgId, boolean isEn) {

        return ecTemplateElementRdbService.filter(param, orgId, isEn);
    }

    @Override
    public List<EcTemplateElementShowResult> getAllElements(EcElementFilterParam param, Long orgId,Boolean isEn) {
        return ecTemplateElementRdbService.getAllElements(param, orgId, isEn);
    }

    @Override
    public Integer getGroupCount(EcElementFilterParam param) {
        return ecTemplateElementRdbService.getGroupCount(param);
    }

    @Override
    public String previewOnTmpl(EcTemplatePreviewElementParam param) {
        EcTemplate ecTemplate = ecTemplateRdbService.findById(param.getTemplateId());
        List<Long> elementIds = new ArrayList<>();
        elementIds.add(param.getFele());
        elementIds.add(param.getSele());
        List<EcTemplateElement> ecTemplateElements = ecTemplateElementRdbService.findByIds(elementIds);

        if(ecTemplate != null && ListUtil.isNotEmpty(ecTemplateElements)) {
            List<EcMaterielElements> elements = new ArrayList<>();
            EcMaterielElements element;
            for(EcTemplateElement obj : ecTemplateElements) {
                element = new EcMaterielElements();
                BeanUtils.copyProperties(obj, element);
                elements.add(element);
            }
            try {
                return feedBuilderService.buildCreativeImg(ecTemplate, elements);
            } catch (Exception e) {
                logger.error("previewOnTmpl在模板上预览元素异常", e);
            }
        }
        return null;
    }

    @Override
    public EcElementAttach getBgDesignEleByTmpl(Long templateId, Long orgId) {
        //找主体背景
        return ecElementAttachRdbService.findBg(templateId, orgId);

    }

    @Override
    public EcElementAttach getBkDesignEleByTmpl(Long templateId, Long orgId) {
        //找装饰
        return ecElementAttachRdbService.findBk(templateId, orgId);
    }

    @Override
    public void updateTmplBgStyle(Long templateId, String templateName, String designCode) {
        String[] templateNameArr = templateName.split(CommonConstants.SYMBOL_UNDERLINE);
        if(templateNameArr.length >= 6)
            templateNameArr[5] = designCode;
        ecTemplateRdbService.updateTmplBgStyle(templateId, arrayToString(templateNameArr), designCode);
    }

    @Override
    public void updateTmplBk(Long templateId, String templateName, String designCode) {
        String[] templateNameArr = templateName.split(CommonConstants.SYMBOL_UNDERLINE);
        String lastName;
        if(templateNameArr.length >= 7) {
            templateNameArr[6] = designCode;
            lastName = arrayToString(templateNameArr);
        } else {
            lastName = templateName + CommonConstants.SYMBOL_UNDERLINE + designCode;
        }

        ecTemplateRdbService.updateTmplBk(templateId, lastName, designCode);
    }

    @Override
    public List<EcTemplate> findAllEcTemplate() {
        return ecTemplateRdbService.findAllEcTemplate();
    }

    private String arrayToString(String[] arr) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < arr.length; i++){
            if(i >0)
                sb.append(CommonConstants.SYMBOL_UNDERLINE);
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    @Override
    public List<EcElementCategory> findBigCatagory(String language) {
        if ("zh-cn".equals(language) || "zh_CN".equals(language) || "zh".equals(language) || "CN".equals(language)) {
            return ecElementCategoryRdbService.findBigCatagoryCN();
        } else  {
            return ecElementCategoryRdbService.findBigCatagoryUS();
        }
    }

    @Override
    public List<EcElementCategory> getElementSmallCategory(String language, Long elementType) {
        if ("zh-cn".equals(language) || "zh_CN".equals(language) || "zh".equals(language) || "CN".equals(language)) {
            return ecElementCategoryRdbService.findSmallCatagoryCN(elementType);
        } else  {
            return ecElementCategoryRdbService.findSmallCatagoryUS(elementType);
        }
    }

    @Override
    public List<EcTemplate> findAllNewEcTemplate(User user) {
        return ecTemplateRdbService.findAllNewEcTemplate(user.getOrgId());
    }
}
